<?php

namespace Home\Controller;

use Think\Controller;

/**
 * 外包客服外呼中心
 */
class UserInvestigationController extends CommnController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function user_mark()
    {
        $link_type = I('link_type');
        $vid = I('vid');
        $data = M('user_surve_mark', '', 'KEFU')->where(['vid' => $vid, 'type' => $link_type])->field('admin_name,ctime,content')->order('ctime asc')->select();
        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
        }
        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
            'type'=>$link_type,
        ));
        $this->display();
    }
    //重审备注
    public function recheck_mark()
    {
        $vid = I('vid');
        $data = M('recheck_anti', '', 'KEFU')->where(['vid' => $vid])->field('content,admin,ctime')->select();
        foreach ($data as $k=>$v){
            if(empty($v['ctime'])){
                $data[$k]['ctime']="";
            }else{
                $data[$k]['ctime']=date('Y-m-d H:i',$v['ctime']);
            }
        }
        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
        ));
        $this->display();
    }

    //重审备注
    public function add_recheck_mark()
    {
        $vid = I('vid');
        $content = I('content');
        $arr=array('ctime'=>time(),'content'=>$content,'admin'=>$_SESSION['name']);
        $update= M('recheck_anti', '', 'KEFU')->where(['vid'=>$vid])->save($arr);
        if($update){
            $this->ajax('1001','success');
        }else{
            $this->ajax('1002','failed');
        }

    }

    public function Remarks()
    {
        $link_type = I('link_type');
        $vid = I('vid');
        $this->assign(array(
            'link_type' => $link_type,
            'vid' => $vid,
        ));
        $this->display();
    }

    public function mark()
    {
        $type = I('type');
        $vid = I('vid');
        $content = I('content');
        $arr = array('admin_name' => $_SESSION['name'], 'content' => $content, 'ctime' => time(), 'vid' => $vid, 'type' => $type);
        $add = M('user_surve_mark', '', 'KEFU')->add($arr);
        if ($add) {
            add_log('对用户' . $vid . '调查备注ID' . $add);
            $this->ajax('1001', '备注成功');
            die;
        }
        $this->ajax('1002', '备注失败');
        die;
    }

    public function quick_mark()
    {
        $vid = I('vid');
        $type = I('type');
        $content = I('content');
        $exit = M('user_surve_flag', '', 'KEFU')->where(['vid' => $vid])->find();
        if (!empty($exit)) {
            M('user_surve_flag', '', 'KEFU')->where(['vid' => $vid])->setfield('flag', $content);
            $this->ajax('1001', '备注成功');
            die;
        }
        $arr = array('admin' => $_SESSION['name'], 'flag' => $content, 'ctime' => time(), 'vid' => $vid, 'type' => $type);
        $add = M('user_surve_flag', '', 'KEFU')->add($arr);
        if ($add) {
            add_log('对用户' . $vid . '外呼备注ID' . $add);
            $this->ajax('1001', '备注成功');
            die;
        }
        $this->ajax('1002', '备注失败');
        die;
    }

    /**
     * 标签
     */
    public function Quick_answer()
    {
        $link_type = I('link_type');
        $vid = I('vid');
        $data = M('kefu_quick_answer', '', 'KEFU')->where(['type' => '2'])->field('title,id')->select();
        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
            'link_type' => $link_type,
        ));
        $this->display();
        die;

    }

    //用户详情
    public function user_info()
    {
        $link_type = I('link_type');
        $vid = I('vid');
        $user = M('user', '', 'USER')->where(['vid' => $vid])->field('vid,mobile,sex,ctime,address,residence_time,id_card')->find();
        $first = substr($user['id_card'], 0, 3);
        $end = substr($user['id_card'], strlen($user['id_card']) - 9, strlen($user['id_card']));
        $user['id_card'] = $first . '******' . $end;
        $user_pic = M('user_idcard_img', '', 'USER')->where(['vid' => $user['vid']])->field('photo,photo1,photo2,front,behind')->find();
        $this->assign(array(
            'user' => $user,
            'vid' => $vid,
            'link_type' => $link_type,
            'user_pic' => $user_pic,
        ));
        $this->display();
    }

    public function borrow_list()
    {
        //要12月份开始    借过款的  给客服拨打电话
        $search = I('user');
        $where = [];
        $where['a.ctime'] = array('egt', 1512057600);
        $where['a.cert_status'] = array('eq', 2);

        if (!empty($search)) {
            $where['a.truename|a.mobile'] = array('like', '%' . $search . '%');
        }
        $sum = M('user', '', 'USER')->join('a join dxd_caiwu.user_borrow b on a.vid=b.vid')->where($where)->group('a.vid')->field('a.id')->select();
        $tol = count($sum);
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = M('user', '', 'USER')->join('a join dxd_caiwu.user_borrow b on a.vid=b.vid')->where($where)->LIMIT($page->listfirst, $page->listRows)->field('a.vid,a.truename,a.ctime,a.mobile,a.sex')->group('a.vid')->select();

        foreach ($data as $k => $v) {
            //是否打通
            $data[$k]['is_call'] = M('user_surve_flag', '', 'KEFU')->where(['vid' => $v['vid'], 'type' => 1])->getField('flag');
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else if ($v['sex'] == 0) {
                $data[$k]['sex'] = '未知';
            }
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->display();
    }

    public function cert_list()
    {
        $search = I('user');
        //要12月份开始   认证通过的客户  给客服拨打电话
        $where = [];
//        $where['a.ctime'] = array('gt',1517414400);
        $time=strtotime(date('Y-m-d',time()));
        $where['a.ctime'] = array('between', array(1517414400,$time));//2.1-3.12
        $where['a.cert_status'] = array('eq', 2);
        $where['b.vid'] = array('exp', 'is null');
        if (!empty($search)) {
            $where['a.truename|b.mobile'] = array('like', '%' . $search . '%');
        }
        $sum = M('user', '', 'USER')->join('a left join dxd_caiwu.user_borrow b on a.vid=b.vid')->where($where)->field('a.id')->select();
        $tol = count($sum);
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = M('user', '', 'USER')->join('a left join dxd_caiwu.user_borrow b on a.vid=b.vid')->where($where)->LIMIT($page->listfirst, $page->listRows)->field('a.vid,a.truename,a.ctime,a.mobile,a.sex')->select();
        foreach ($data as $k => $v) {
            //是否打通
            $data[$k]['is_call'] = M('user_surve_flag', '', 'KEFU')->where(['vid' => $v['vid'], 'type' => 2])->getField('flag');
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else if ($v['sex'] == 0) {
                $data[$k]['sex'] = '未知';
            }
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->display();
    }

    /**
     *用户注册未认证列表  11月份
     */
    public function noCertList()
    {
        $key=I('user');
        $where=[];
        $where['a.cert_status']=array('eq',1);
        $where['a.type']=array('eq',3);
        //两天前的时间戳
        $two_days=time()-2*24*3600;
        $where['a.ctime']=array('between',array(1517414400,$two_days));//2017/11/1 0:0:0
//        $where['a.ctime']=array('gt',1514736000);//2017/11/1 0:0:0
        if(!empty($key)){
            $where['a.mobile|a.truename']=array('like','%'.$key.'%');
        }
        $where['b.vid']=array('exp','is null');
//        $where['c.vid']=array('exp','is null');
//        $where['c.ctime']=array('lt',time()-7*24*3600);
//        $tol=M('user','','USER')->join('a left join dxd_kefu.kefu_ownuser b on a.vid=b.vid and b.type=3')->join('left join dxd_kefu.call_info c on c.vid=a.vid')->where($where)->count('DISTINCT a.vid');
        $tol=M('user','','USER')->join('a left join dxd_kefu.kefu_ownuser b on a.vid=b.vid and b.type=3')->where($where)->count('DISTINCT a.vid');
        $row = '100';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();

//        $data=M('user','','USER')->join('a left join dxd_kefu.kefu_ownuser b on a.vid=b.vid and b.type=3')->join('left join dxd_kefu.call_info c on c.vid=a.vid')->where($where)->field('a.id,a.ctime,a.vid,a.mobile,a.sex,a.truename')->order('a.ctime desc')->group('a.vid')->limit($page->listfirst, $page->listRows)->select();
        $data=M('user','','USER')->join('a left join dxd_kefu.kefu_ownuser b on a.vid=b.vid and b.type=3')->where($where)->field('a.id,a.ctime,a.vid,a.mobile,a.sex,a.truename')->order('a.ctime desc')->group('a.vid')->limit($page->listfirst, $page->listRows)->select();

        foreach ($data as $k => $v) {
            //最后沟通记录
            $content = M('call_info', '', 'KEFU_R')->where(['vid' => $v['vid']])->order('ctime desc')->limit(1)->field('content')->find();

            $data[$k]['is_call'] = $content['content'];
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else if ($v['sex'] == 0) {
                $data[$k]['sex'] = '未知';
            }
        }
        //客服列表
        $where=[];
        $where['out_kf_id']=0;
        $where['status']=1;
        $where['part_id']=1;
        $where['role_id']=2;
        $kf = M('admin', '', 'KEFU')->where($where)->field('name,id as kf_id')->select();
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'page_first' => $page->listfirst,
            'tol' => $tol,
            'kefu' => $kf,
        ));
        $this->display();
    }
    /**
     *重审用户 93
     */
    public function recheck()
    {


        $tol=M('recheck_anti','','KEFU')->join('a join dxd_user.user b on a.id=b.id')->where(['b.cert_status'=>2])->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data=M('recheck_anti','','KEFU')->join('a join dxd_user.user b on a.id=b.id')->where(['b.cert_status'=>2])->field('b.ctime,b.vid,b.mobile,b.sex,b.truename,b.cert_status,a.content')->limit($page->listfirst, $page->listRows)->select();
        foreach ($data as $k => $v) {
            //最后沟通记录
//            $data[$k]['is_call'] = M('user_surve_mark', '', 'KEFU')->where(['vid' => $v['vid'], 'type' => 3])->limit(1)->order('ctime desc')->getField('content');
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else if ($v['sex'] == 0) {
                $data[$k]['sex'] = '未知';
            }
//基础认证，1未认证，2已通过，3未通过4.审核中
              if ($v['cert_status'] == 1) {
                $data[$k]['cert_status'] = '未认证';
            } else if ($v['cert_status'] == 2) {
                $data[$k]['cert_status'] = '已通过';
            } else if ($v['cert_status'] == 3) {
                $data[$k]['cert_status'] = '未通过';
            } else if ($v['cert_status'] == 4) {
                $data[$k]['cert_status'] = '审核中';
            }

        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));

        $this->display();
    }

    /**
     *一次以上借款用户
     */
    public function borrowed_user(){
        $page_first = I('page_first');
        $this->page = I('page', 0);

        $tol=M('user_borrow','','KEFU')->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        if (!empty($page_first)) {
            $page->listfirst = ( $this->page-1)*$row ;
        }

        $field="b.mobile,b.truename,b.sex,b.ctime,a.is_call,a.admin,b.vid";
        $data=M('user_borrow','','KEFU')->join('a join dxd_user.user b on a.vid=b.vid')->field($field)->limit($page->listfirst, $page->listRows)->select();
       foreach ($data as $k=>$v){
        $data[$k]['ctime']=date('Y-m-d H:i:s',$v['ctime']);
        if($v['sex']=='1'){
            $data[$k]['sex']='男';
        }else   if($v['sex']=='2'){
            $data[$k]['sex']='女';
        }
        if($v['is_call']==2){
            $data[$k]['is_call']='已接通';
        }else if($v['is_call']==1){
            $data[$k]['is_call']='未接通';
        }else{
            $data[$k]['is_call']=' ';
        }
       }
        $this->assign(array(
            'data' => $data,
            'page' =>  $this->page,
            'fpage' => $fpage,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->display();
    }

    //一次以上借款用户是否接通
    public function is_get(){
        $status=I('status');
        $vid=I('vid');
        $arr=array('is_call'=>$status,'admin'=>$_SESSION['name'],'ctime'=>time());
        $update=M('user_borrow','','KEFU')->where(['vid'=>$vid])->save($arr);

        if($update){
            $this->ajax('1001','success');
            die;
        }
        $this->ajax('1002','failed');
        die;
    }




}